﻿using MediatR;
using ProcurementManagement.Applcation.Command.PurchaseContractsCommand;
using ProcurementManagement.Domain.Dtos.PurchaseContracts;
using ProcurementManagement.Domain.ProcurementDomain;
using ProcurementManagement.Infrstructre.Interface;
using SqlSugar;

namespace ProcurementManagement.Applcation.CommandHanlers.PurchaseContractsCommandHandler
{
    /// <summary>
    /// 采购合同反填
    /// </summary>
    public class PurchaseContractsFanCommandHandler:IRequestHandler<PurchaseContractsFanCommand, List<FanPurchaseContractsDTO>>
    {
        private readonly IPurchaseContractsRepository _purchaseContractsRep;
        private readonly ISourcProductContractRepository _sourceProductContractRep;
        ISqlSugarClient db;

        public PurchaseContractsFanCommandHandler(IPurchaseContractsRepository purchaseContractsRep, ISourcProductContractRepository sourceProductContractRep, ISqlSugarClient db)
        {
            this._purchaseContractsRep = purchaseContractsRep;
            this._sourceProductContractRep = sourceProductContractRep;
            this.db = db;
        }

        public async Task<List<FanPurchaseContractsDTO>> Handle(PurchaseContractsFanCommand request, CancellationToken cancellationToken)
        {
            var res = db.Queryable<PurchaseContracts, SourcProductContract,Vendor>((purchaseContracts, sourceProductContract,vendor) => new JoinQueryInfos(
                JoinType.Inner, purchaseContracts.PCNumber == sourceProductContract.PCNumber,
                JoinType.Inner, purchaseContracts.VendorId == vendor.VendorId))
                .Where((purchaseContracts, sourceProductContract,vendor) => purchaseContracts.PurchaseContractsIds == request.PurchaseContractsIds)
                .Select((purchaseContracts, sourceProductContract,vendor) => new FanPurchaseContractsDTO
                {
                    PurchaseContractsIds = purchaseContracts.PurchaseContractsIds,
                    VendorId = purchaseContracts.VendorId,
                    PCNumber = purchaseContracts.PCNumber,
                    PCName = purchaseContracts.PCName,
                    PCTypes = purchaseContracts.PCTypes,
                    PCWhetherQuality = purchaseContracts.PCWhetherQuality,
                    ManpowerId = purchaseContracts.ManpowerId,
                    PCOurRepresentatives = purchaseContracts.PCOurRepresentatives,
                    PCRepresentParty = purchaseContracts.PCRepresentParty,
                    ProcureApplyId = purchaseContracts.ProcureApplyId,
                    PContractsDate = purchaseContracts.PContractsDate,
                    PCPreparationDate = purchaseContracts.PCPreparationDate,
                    PCProductPrice = purchaseContracts.PCProductPrice,
                    PCPreferentialPrice = purchaseContracts.PCPreferentialPrice,
                    PCAfterOfferPrice = purchaseContracts.PCAfterOfferPrice,
                    AddName = purchaseContracts.AddName,
                    SourcProductContractId = sourceProductContract.SourcProductContractId,
                    ProductId = sourceProductContract.ProductId,
                    SPCNum = sourceProductContract.SPCNum,
                    SPCPrice = sourceProductContract.SPCPrice,
                    SPCDocumentsType = sourceProductContract.SPCDocumentsType,
                    SPCTotalPrice = sourceProductContract.SPCTotalPrice,
                    SPCAfterPrice = sourceProductContract.SPCAfterPrice,
                    SPCAfterTotalPrice = sourceProductContract.SPCAfterTotalPrice,
                    SPCArrivalDate = sourceProductContract.SPCArrivalDate,
                    SPCRemark = sourceProductContract.SPCRemark,
                    SPPNumber=sourceProductContract.SPPNumber,
                    SPPName=sourceProductContract.SPPName,
                    SPPSpec = sourceProductContract.SPPSpec,
                    VNumber=vendor.VNumber,
                    VName=vendor.VName,
                }).ToList();
            return res.ToList();
        }
    }
}
